<template>
  <div id="petsList">
    <div class="gva-search-box">
      <el-form :inline="true" :model="searchInfo">
        <el-form-item>
          <el-input v-model="searchInfo.petsName" placeholder="搜索条件" />
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button size="small" icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="gva-table-box">
      <div class="gva-btn-list" />
      <el-table
        :data="tableData"
      >
        <el-table-column label="用户ID" min-width="100" prop="id" />
        <el-table-column label="宠物昵称" min-width="100" prop="name" />
        <el-table-column label="宠物简介" min-width="100" prop="describe" />
        <el-table-column label="宠物性别" min-width="100" prop="category_id">
          <template #default="scope">
            <span>{{ scope.row.gender === 1 ? "男" : "女" }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="demo-pagination-block">
        <div class="demonstration">All combined</div>
        <el-pagination
          v-model:current-page="page"
          v-model:page-size="pageSize"
          :page-sizes="[100, 200, 300, 400]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="400"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>

import { ref } from 'vue'
import { getPetsList } from '@/api/pets'

const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})

const small = ref(false)
const background = ref(true)
const disabled = ref(false)

const handleSizeChange = (val) => {
  console.log('handleSizeChange-val:' + val)
  pageSize.value = val
  getTableData()
}

const handleCurrentChange = (val) => {
  console.log('handleCurrentChange-val:' + val)
  page.value = val
  pageSize.value = 10
  getTableData()
}

// 获取宠物列表
const getTableData = async() => {
  const data = await getPetsList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (data.code === 0) {
    tableData.value = data.data.list
    total.value = data.total
    page.value = data.page
    pageSize.value = data.pageSize
  } else {
    console.log('data is null')
  }
}

const onReset = () => {
  searchInfo.value = {}
}

const onSubmit = () => {
  page.value = 1
  pageSize.value = 10
  if (searchInfo.value.petsName === '') {
    searchInfo.value.petsName = null
  }
  getTableData()
}

getTableData()

</script>
